{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6c820be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "# python中单行注释\n",
    "print(\"hello world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "427d9b60",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "#缩进\n",
    "if True:\n",
    "    print(\"True\")\n",
    "else:\n",
    "    print(\"False\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50064b1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#多行语句\n",
    "total = item_one + \\\n",
    "        item_two + \\\n",
    "        item_three"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3ff5ef86",
   "metadata": {},
   "outputs": [],
   "source": [
    "days=['Monday','Tuesday','Wednesday','Thursday','Friday']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "08bab280",
   "metadata": {},
   "outputs": [],
   "source": [
    "word='word'\n",
    "sentence=\"这是一个句子\"\n",
    "paragraph=\"\"\"这是一个段落。\n",
    "#包含了多个语句\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "66c8bb35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "#input输入\n",
    "a=input('请输入任意键结束：')\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "10da0e48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "runoob\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#同一行显示多条语句\n",
    "import sys;x='runoob';sys.stdout.write(x+'\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57faa9ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "#多个语句构成代码组\n",
    "if expression:\n",
    "    suite\n",
    "elif expression:\n",
    "    suite\n",
    "else:\n",
    "    suite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0f4b096a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#变量赋值\n",
    "a,b,c=1,2,\"john\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b4a54228",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n",
      "H\n",
      "llo\n",
      "llo World!\n",
      "Hello World!Hello World!\n",
      "Hello World!TEST\n"
     ]
    }
   ],
   "source": [
    "#Python字符串\n",
    "str='Hello World!'\n",
    "\n",
    "print(str)              #输出完整字符串\n",
    "print(str[0])           #输出字符串中的第一个字符\n",
    "print(str[2:5])         #输出字符串中第三个至第六个之间的字符串\n",
    "print(str[2:])          #输出从第三个字符开始的字符串\n",
    "print(str * 2)          #输出字符串两次\n",
    "print(str + \"TEST\")     #输出连接的字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fed6894b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['runoob', 786, 2.23, 'john', 70.2]\n",
      "runoob\n",
      "[786, 2.23]\n",
      "[2.23, 'john', 70.2]\n",
      "[123, 'john', 123, 'john']\n",
      "['runoob', 786, 2.23, 'john', 70.2, 123, 'john']\n",
      "john\n"
     ]
    }
   ],
   "source": [
    "#Python列表\n",
    "list=['runoob',786,2.23,'john',70.2]\n",
    "tinylist=[123,'john']\n",
    "\n",
    "print(list)                 #输出完整列表\n",
    "print(list[0])              #输出列表的第一个元素\n",
    "print(list[1:3])            #输出第二个至第三个元素\n",
    "print(list[2:])             #输出从第三个开始至列表末尾的所有元素\n",
    "print(tinylist * 2)         #输出列表两次\n",
    "print(list + tinylist)      #打印组合的列表\n",
    "print(list[-2])             #输出从第一个开始至第二个元素(反向输出)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4c72836",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Python元组\n",
    "tupe=('runoob',786,2.23,'john',70.2)\n",
    "list=['runoob',786,2.23,'john',70.2]\n",
    "tupe[2]=1000    #元组中是非法应用(元组不允许更新)\n",
    "list[2]=1000    #列表中是合法应用(列表允许更新)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab9f6039",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is one\n",
      "This is two\n",
      "{'name': 'runoob', 'code': 6734, 'dept': 'sales'}\n",
      "dict_keys(['name', 'code', 'dept'])\n",
      "dict_values(['runoob', 6734, 'sales'])\n"
     ]
    }
   ],
   "source": [
    "#Python字典\n",
    "dict={}\n",
    "dict['one']=\"This is one\"   #键值对\n",
    "dict[2]=\"This is two\"\n",
    "\n",
    "tinydict={'name':'runoob','code':6734,'dept':'sales'}\n",
    "\n",
    "print(dict['one'])           #输出键为'one'的值\n",
    "print(dict[2])               #输出键为2的值\n",
    "print(tinydict)              #输出完整的字典\n",
    "print(tinydict.keys() )      #输出所有键\n",
    "print(tinydict.values())     #输出所有值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3c1396f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "大一点\n",
      "小一点\n",
      "大一点\n",
      "小一点\n",
      "恭喜你猜对了\n"
     ]
    }
   ],
   "source": [
    "#猜数字游戏：计算机产生一个1～100的随机整数让玩家来猜测，玩家输入自己猜测的数字，\n",
    "#计算机给出相应的提示：小一点、大一点、恭喜你猜对了。用户猜出这个数字，游戏结束。\n",
    "#如果用户猜了7次以上，计算给出提示：智商余额明显不足。\n",
    "import random\n",
    "target=random.randint(1,100)\n",
    "for i in range(1,8):\n",
    "    guess=int(input(\"请输入你猜测的值\"))\n",
    "    if guess<target:\n",
    "        print(\"大一点\")\n",
    "    elif guess>target:\n",
    "        print(\"小一点\")\n",
    "    else:\n",
    "        print(\"恭喜你猜对了\")\n",
    "        break\n",
    "else:\n",
    "    print(\"智商余额明显不足\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e360a964",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100以内的所有质数:\n",
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\n"
     ]
    }
   ],
   "source": [
    "# 1、求n以内的所有质数\n",
    "def is_Prime(n):\n",
    "    if n<=1:\n",
    "        return False\n",
    "    if n==2:\n",
    "        return True\n",
    "    if n%2==0:\n",
    "        return False\n",
    "    for i in range(3,int(n**0.5)+1,2):\n",
    "        if n%i==0:\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "def find_primes(n):\n",
    "    primes = []\n",
    "    for num in range(2, n + 1):\n",
    "        if is_Prime(num):\n",
    "            primes.append(num)\n",
    "    return primes\n",
    "\n",
    "limit = 100\n",
    "prime_list = find_primes(limit)\n",
    "print(f\"{limit}以内的所有质数:\")\n",
    "print(prime_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fa9020c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的水仙花数：[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]\n"
     ]
    }
   ],
   "source": [
    "# 2、求n以内的水仙花数\n",
    "def narcissistic_numbers(n):\n",
    "    result = []\n",
    "    for num in range(1, n + 1):\n",
    "        # 将数字转换为字符串以便获取每一位\n",
    "        digits = str(num)\n",
    "        power = len(digits)  # 位数\n",
    "        \n",
    "        # 计算各位数字的幂次和\n",
    "        sum_of_powers = sum(int(digit) ** power for digit in digits)\n",
    "        \n",
    "        # 判断是否为水仙花数\n",
    "        if sum_of_powers == num:\n",
    "            result.append(num)\n",
    "    \n",
    "    return result\n",
    "\n",
    "print(f\"1000以内的水仙花数：{narcissistic_numbers(1000)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2c12b6dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "上0个台阶的走法数量: 1种\n",
      "上1个台阶的走法数量: 1种\n",
      "上2个台阶的走法数量: 2种\n",
      "上3个台阶的走法数量: 3种\n",
      "上5个台阶的走法数量: 8种\n",
      "上10个台阶的走法数量: 89种\n",
      "上20个台阶的走法数量: 10946种\n"
     ]
    }
   ],
   "source": [
    "# 3、假设有n个台阶，每跨一步只能上1阶或者2阶台阶。求总共有多少种走法\n",
    "def climb_stairs(n):\n",
    "    \"\"\"\n",
    "    计算上n个台阶的走法数量，每次只能上1阶或2阶\n",
    "    \n",
    "    参数:\n",
    "    n -- 台阶数量\n",
    "    \n",
    "    返回:\n",
    "    走法数量\n",
    "    \"\"\"\n",
    "    if n <= 1:\n",
    "        return 1\n",
    "    a, b = 1, 1  # a表示n-2阶的走法，b表示n-1阶的走法\n",
    "    for _ in range(2, n + 1):\n",
    "        a, b = b, a + b  # 更新为当前n阶的走法\n",
    "    return b\n",
    "\n",
    "# 测试函数\n",
    "test_cases = [0, 1, 2, 3, 5, 10, 20]\n",
    "for n in test_cases:\n",
    "    print(f\"上{n}个台阶的走法数量: {climb_stairs(n)}种\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1064069e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三正在教授数学\n",
      "李四正在学习1班\n"
     ]
    }
   ],
   "source": [
    "class Teacher:\n",
    "    def __init__(self) -> None:\n",
    "        self.name=None\n",
    "        self.age=None\n",
    "        self.number=None\n",
    "        self.subject=None\n",
    "\n",
    "    def teach(self):\n",
    "        print(f'{self.name}正在教授{self.subject}')\n",
    "\n",
    "\n",
    "class Student:\n",
    "    def __init__(self) -> None:\n",
    "        self.name=None\n",
    "        self.age=None\n",
    "        self.number=None\n",
    "        self.class_num=None\n",
    "\n",
    "    def study(self):\n",
    "        print(f'{self.name}正在学习{self.class_num}')\n",
    "\n",
    "\n",
    "t1=Teacher()\n",
    "t1.name='张三'\n",
    "t1.age=30\n",
    "t1.number='001'\n",
    "t1.subject='数学'\n",
    "t1.teach()\n",
    "\n",
    "s1=Student()\n",
    "s1.name='李四'\n",
    "s1.age=18\n",
    "s1.number='002'\n",
    "s1.class_num='1班'\n",
    "s1.study()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "200e6e54",
   "metadata": {},
   "outputs": [],
   "source": [
    "#列表推导式\n",
    "#li=[1,2,...,34]\n",
    "all_red_balls=[x for x in range(1,34)]\n",
    "all_blu_balls=[x for x in range(1,17)]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Project",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
